package com.zhonghui.dolo.manager.dao.impl;

import com.zhonghui.dolo.manager.dao.GoodsCategoryDao;
import com.zhonghui.dolo.manager.dao.mapper.GoodsCategoryMapper;
import com.zhonghui.dolo.manager.model.GoodsCategory;
import com.zhonghui.dolo.manager.model.GoodsCategoryExample;
import com.zhonghui.dolo.manager.util.Constans;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.List;

@Repository
public class GoodsCategoryDaoImpl implements GoodsCategoryDao {

    @Autowired
    private GoodsCategoryMapper goodsCategoryMapper;

    @Override
    public GoodsCategory getCategoryById(Integer categoryId) {
        return goodsCategoryMapper.selectByPrimaryKey(categoryId);
    }

    @Override
    public List<GoodsCategory> findCategoryListByParentId(Integer parentId) {
        GoodsCategoryExample example = new GoodsCategoryExample();
        example.createCriteria().andParentIdEqualTo(parentId)
                .andStateNotEqualTo(Constans.State.DEL);
        return goodsCategoryMapper.selectByExample(example);
    }

    @Override
    public List<GoodsCategory> findCategoryList(String categoryName) {
        GoodsCategoryExample example = new GoodsCategoryExample();
        GoodsCategoryExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(categoryName)) {
            criteria.andCategoryNameLike("%" +categoryName+ "%");
        }
        criteria.andStateNotEqualTo(Constans.State.DEL);
        return goodsCategoryMapper.selectByExample(example);
    }

    @Override
    public int add(GoodsCategory category) {
        return goodsCategoryMapper.insertSelective(category);
    }
}
